package com.lgj.springboot_lgj.web.controller;

import com.github.pagehelper.PageInfo;
import com.lgj.springboot_lgj.aop.Logging;
import com.lgj.springboot_lgj.bean.basic.BaseConfig;
import com.lgj.springboot_lgj.dao.basic.BaseConfigMapper;
import com.lgj.springboot_lgj.dao.ext.BaseConfigExtMapper;
import com.lgj.springboot_lgj.exception.CustomerException;
import com.lgj.springboot_lgj.service.IBaseConfigService;
import com.lgj.springboot_lgj.utils.EntityUtil;
import com.lgj.springboot_lgj.utils.Result;
import com.lgj.springboot_lgj.utils.ResultUtil;
import com.lgj.springboot_lgj.web.vm.ConfigInsertVm;
import com.lgj.springboot_lgj.web.vm.ConfigUpdateVm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author:Lijun
 * @Date:2022/6/19
 */
@Api(tags = "配置信息模块")
@RestController
@RequestMapping("/auth/config")
public class ConfigController {
    @Autowired
    private IBaseConfigService service;
    @Logging
    @GetMapping("/page/query")
    @ApiOperation("分页查询所有配置信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true, dataType = "int")})
    public Result page(Integer pageNum, Integer pageSize) {
        PageInfo<BaseConfig> pageInfo = service.findAllWithPage(pageNum, pageSize);
        return ResultUtil.success(pageInfo);
    }
    @Logging
    @DeleteMapping("/deleteById")
    @ApiOperation("通过id删除配置信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "编号", required = true, dataType = "int")})
    public Result deleteById(Integer id) {
        service.deleteById(id);
        return ResultUtil.success("删除成功！");
    }

    @Logging
    @GetMapping("/current/config/yes")
    @ApiOperation("查找当前启用的系统配置")
    public Result current() {
        BaseConfig baseConfig = service.currentConfigYes();
        return ResultUtil.success(baseConfig);
    }

    @Logging
    @PutMapping("update/status")
    @ApiOperation("更新配置信息状态 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "编号", required = true, dataType = "int"),
            @ApiImplicitParam(name = "status", value = "状态，0-禁用，1-启用", required = true, dataType = "int")})
    public Result changeConfigStatus(int id, int status) {
        service.changeConfigStatus(id, status);
        return ResultUtil.success("配置信息状态修改成功");
    }

    @Logging
    @PostMapping("/save")
    @ApiOperation("保存配置信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "configStatus", value = "状态，0-禁用，1-启用", required = true, dataType = "int"),
            @ApiImplicitParam(name = "configIcon", value = "配置信息图标地址", required = true),
            @ApiImplicitParam(name = "configInfo", value = "配置信息描述", required = true),
            @ApiImplicitParam(name = "configName", value = "配置名称", required = true)
    })
    public Result saveConfig(ConfigInsertVm vm) {
        service.saveConfig(EntityUtil.vm2Entity(vm, BaseConfig.class));
        return ResultUtil.success("保存成功");

    }

    @Logging
    @PutMapping("/update")
    @ApiOperation("更新配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "configId", value = "配置编号", required = true, dataType = "int"),
            @ApiImplicitParam(name = "configIcon", value = "配置信息图标地址", required = true),
            @ApiImplicitParam(name = "configInfo", value = "配置信息描述", required = true),
            @ApiImplicitParam(name = "configName", value = "配置名称", required = true)
    })
    public Result update(ConfigUpdateVm vm) {
        BaseConfig baseConfig = EntityUtil.vm2Entity(vm, BaseConfig.class);
        service.updateConfig(baseConfig);
        return ResultUtil.success("更新成功");
    }

}
